﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UniMana.Data
{
    public class System_RuleData
    {
        public static bool CheckExist(string _strRuleID)
        {
            bool result = false;
            DBMain m_data = new DBMain();
            var Iquery = m_data.m_DCSave.System_Rules.Where(p => p.RuleID == _strRuleID).FirstOrDefault();
            if (Iquery != null)
            {
                result = true;
            }

            return result;
            
        }

       // private static DBMain m_data = new DBMain();

        public static System_User_Rule CheckRoleByUsername(DBMain m_data, string _strUsername, string _strRuleID)
        {
            //if (_strRule == "rpgAccept")
            //{
            //    string str = "rpgAccept";
            //    MessageBox.Show(str);
            //}
            
            System_User_Rule iQuery = (
                         from su in m_data.m_DCReadOnly.System_Users
                         from sur in m_data.m_DCReadOnly.System_User_Rules
                         from sp in m_data.m_DCReadOnly.System_Rules
                         where su.UserName == _strUsername &&
                         (sp.RuleID == _strRuleID) && //sp.RuleName == _strRule ||
                          su.System_UserKey == sur.System_UserKey &&
                          sur.System_RuleKey == sp.System_RuleKey
                         select sur).FirstOrDefault();
            return iQuery;
        }

        public static IQueryable RuleByGroup(DBMain m_data, int _intGroupKey)
        {
            var iQuery = from sr in m_data.m_DCSave.System_Rules
                         from sgr in m_data.m_DCSave.System_Group_Rules
                         where
                         sgr.System_GroupKey == _intGroupKey &&
                         sr.System_RuleKey == sgr.System_RuleKey
                         select new {
                             sr.System_RuleKey, 
                             sr.RuleID, 
                             sr.RuleName,
                             sr.RuleNameVi,
                             sr.Description,
                             sr.Level,
                             sr.ParentID,
                             sr.Owner,
                             sgr.AllowAccess,
                             sgr.AllowAdd,
                             sgr.AllowDelete,
                             sgr.AllowEdit,
                             sgr.AllowExport,
                             sgr.AllowImport,
                             sgr.AllowPrint,
                             sgr.AllowSave
                         };
            return iQuery.OrderBy(p=>p.Level);
        }
        public static IQueryable RuleOfGroup(DBMain m_data, int _intGroupKey)
        {
            var iQuery = from sr in m_data.m_DCSave.System_Rules
                         from sgr in m_data.m_DCSave.System_Group_Rules
                         where
                         sgr.System_GroupKey == _intGroupKey &&
                         sr.System_RuleKey == sgr.System_RuleKey
                         select new { sr.RuleID, sr.RuleName, sr.RuleNameVi, sr.Description, sr.Level, sr.ParentID, sr.Owner };
            return iQuery.OrderBy(p => p.Level);
        }
        public static IQueryable RuleList()
        {
            DBMain db = new DBMain();
            return db.m_DCReadOnly.System_Rules;

        }
        public static IQueryable PermissionRuleOfGroup()
        {
            DBMain db = new DBMain();
            var iQuery = from sr in db.m_DCReadOnly.System_Rules
                         from sgr in db.m_DCReadOnly.System_Group_Rules
                         from sg in db.m_DCReadOnly.System_Groups
                         where
                         sr.System_RuleKey == sgr.System_RuleKey
                         && sg.System_GroupKey == sgr.System_GroupKey
                         select new
                         {
                             sr.System_RuleKey,
                             sr.RuleID,
                             sr.RuleName,
                             sr.RuleNameVi,
                             GroupName = sg.Name,
                             sr.Description,
                             sr.ParentID,
                             sgr.AllowAccess,
                             sgr.AllowAdd,
                             sgr.AllowDelete,
                             sgr.AllowEdit,
                             sgr.AllowExport,
                             sgr.AllowImport,
                             sgr.AllowPrint,
                             sgr.AllowSave
                         };
            //var iQuery = from sr in db.m_DCReadOnly.System_Rules
            //             //from sgr in db.m_DCReadOnly.System_Group_Rules
            //             //from sg in db.m_DCReadOnly.System_Groups
            //             //where
            //             //sr.System_RuleKey == sgr.System_RuleKey
            //             //&& sg.System_GroupKey == sgr.System_GroupKey
            //             select new
            //             {
            //                 sr.System_RuleKey,
            //                 sr.RuleID,
            //                 sr.RuleName,
            //                 sr.RuleNameVi,
            //                 GroupName = "",
            //                 sr.Description,
            //                 sr.ParentID,
            //                 AllowAccess = true,
            //                 AllowAdd = true,
            //                 AllowDelete = true,
            //                 AllowEdit = true,
            //                 AllowExport = true,
            //                 AllowImport = true,
            //                 AllowPrint = true,
            //                 AllowSave = true
            //             };
            

            return iQuery;
        }
    }
}
